﻿using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Galaxy.AutoCAD.Geometrys;

namespace Galaxy.AutoCAD.Databases
{
    public static class EntityTool
    {
        #region 获得实体的中心点（定义为entity的括展函数）
        public static Point3d GetEntityCentre(this Entity ent)
        {
            Extents3d entExt = ent.Bounds.Value;
            return Point3dTool.GetCentrePoint(entExt.MinPoint, entExt.MaxPoint);
        }
        #endregion
        #region 移动实体（定义为entity的括展函数）
        public static void Move(this Entity ent, Point3d sourcePt, Point3d targetPt)
        {
            if (ent.IsNewObject)
            {
                Vector3d vector = targetPt - sourcePt;
                Matrix3d mt = Matrix3d.Displacement(vector);
                ent.TransformBy(mt);
            }
            else
            {
                ent.ObjectId.Move(sourcePt, targetPt);
            }
        }
        #endregion
        #region 删除实体
        public static void Remove(this Entity ent)
        {
            Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction tran = db.TransactionManager.StartTransaction())
            {
                Entity targetEntity = tran.GetObject(ent.ObjectId, OpenMode.ForWrite) as Entity;
                targetEntity.Erase();
                tran.Commit();
            }
        }
        #endregion
    }
}
